import java.util.ArrayList;
import java.util.List;

public class Test {

}
class Solution4 {
    public int passThePillow(int n, int time) {
        int ans = 1;
        while(time != 0){
            while(time!=0 && ans<n){
                time--;
                ans++;
            }
            while(time!=0 && ans>1){
                time--;
                ans--;
            }
        }
        return ans;
    }
}
class Solution {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int i=0; i<nums.length; i++){
            sum += nums[i];
        }
        int target = sum - x;
        if(target < 0) return -1;
        if(target == 0) return nums.length;
        int ans = 0;
        sum = 0;
        for(int l=0, r=0; r<nums.length; r++){
            sum += nums[r];
            while(sum > target){
                sum -= nums[l++];
            }
            if(sum == target){
                ans = Math.max(ans,r-l+1);
            }
        }
        if(ans == 0) return -1;
        return nums.length - ans;
    }
}
class Solution1 {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        int[] t = new int[n];
        int count = 0;
        int ans = 0;
        for(int i=0, j=0; i<n; i++){
            t[fruits[i]]++;
            if(t[fruits[i]] == 1) count++;
            if(count >= 3){
                while(t[fruits[j]]!=1){
                    t[fruits[j]]--;
                    j++;
                }
                t[fruits[j++]]--;
                count--;
            }
            ans = Math.max(ans,i-j+1);
        }
        return ans;
    }
}
class Solution2 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ans = new ArrayList<>();
        int[] t1 = new int[26];
        int[] t2 = new int[26];
        int cnt = p.length();
        for(char ch : p.toCharArray()){
            t1[ch-'a']++;
        }
        for(int l=0,r=0,count=0; r<s.length(); r++){
            char ch = s.charAt(r);
            if(++t2[ch-'a'] <= t1[ch-'a']){
                count++;
            }
            if(r-l+1 > p.length()){
                char out = s.charAt(l++);
                if(t2[out-'a']--<=t1[out-'a']) count--;
            }
            if(count == cnt) ans.add(l);
        }
        return ans;
    }
}
